package logic

import (
	"context"
	"fmt"
	"hotel/model/mysql"
	"hotel/model/redisinit"
	"hotel/pkg"

	"hotel/userrpc/internal/svc"
	"hotel/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserUpLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserUpLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUpLogic {
	return &UserUpLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserUpLogic) UserUp(in *userrpc.UserUpRequest) (*userrpc.UserUpResponse, error) {
	// todo: add your logic here and delete this line

	var finduser mysql.User
	mysql.DB.Debug().Where("user_name = ?", in.Username).Find(&finduser)
	if finduser.ID == 0 {
		return nil, fmt.Errorf("该账号不存在")
	}
	if finduser.Password != pkg.Md5(in.Password) {
		return nil, fmt.Errorf("密码错误")
	}
	get := redisinit.Rdb.Get("sendSms" + in.Tel + "userup")
	if in.SmsCode != get.Val() {
		return nil, fmt.Errorf("短信验证码错误，请重试")
	}

	redisinit.Rdb.Del("sendSms" + in.Tel + "userup")

	return &userrpc.UserUpResponse{
		Id: int64(finduser.ID),
	}, nil
}
